//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.KyCard;
import com.aidong.model.wills.MemberCard;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

@Repository
public class KyCardDao extends BaseDao<KyCard, String> {
    public KyCardDao() {
    }

    public KyCard getById(String cardCode) {
        return this.getByCardCode(cardCode);
    }

    public KyCard getByCardCode(String cardCode) {
        return (KyCard)this.getByWhere("cardCode=:p1", new Object[]{cardCode});
    }

    public boolean chkCardExists(String code1, String code2) {
        String sql = "select count(*) from KyCard where cardCode>=:p1 and cardCode<=:p2";
        return ((Integer)this.getBySql(Integer.class, sql, new Object[]{code1, code2})).intValue() > 0;
    }

    public List<KyCard> getNotUseList(String shopCode) {
        return this.queryList("shopCode=:p1 and status=1", new Object[]{shopCode}, (new OrderSet()).asc("cardCode"), -1);
    }

    public List<MemberCard> getNotUseTypeCount(String shopCode) {
        String sql = "select left(cardCode,1) cardType,count(*) status from KyCard\nwhere shopCode=:p1 and status=1 group by left(cardCode,1) order by cardType";
        return this.queryListBySql(MemberCard.class, sql, new Object[]{shopCode});
    }

    public void addCardList(List<String> cardList, String shopCode, String userCode) {
        String sql = "insert into KyCard (CardCode, ShopCode, Status, CreateUser, CreateTime, UpdateTime) values (:p1, :p2, 1, :p3, getdate(), getdate())";
        SQLQuery query = this.getSession().createSQLQuery(sql);
        query.setString("p2", shopCode);
        query.setString("p3", userCode);
        Iterator i$ = cardList.iterator();

        while(i$.hasNext()) {
            String cardCode = (String)i$.next();
            query.setString("p1", cardCode);
            query.executeUpdate();
        }

    }

    public void delCardList(List<String> cardList) {
        String sql = "delete from KyCard where CardCode = :p1 and status=1";
        SQLQuery query = this.getSession().createSQLQuery(sql);
        Iterator i$ = cardList.iterator();

        while(i$.hasNext()) {
            String cardCode = (String)i$.next();
            query.setString("p1", cardCode);
            query.executeUpdate();
        }

    }

    public void updateReceive(KyCard card) {
        String sql = "update KyCard set status=:p1, receiveUser=:p2, receiveDate=:p3 where cardCode=:p4";
        this.updateBySql(sql, new Object[]{card.getStatus(), card.getReceiveUser(), card.getReceiveDate(), card.getCardCode()});
    }
}
